package com.deeper.customview.ui.c4.demo.c04d07;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

public class ComposeShaderView extends View {


    private Paint paint;


    public ComposeShaderView(Context context) {
        super(context);
    }


    public ComposeShaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
    }


    public ComposeShaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        BitmapShader bg = new BitmapShader(bmp, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
        LinearGradient lg = new LinearGradient(0, 0, getMeasuredWidth(), 600, Color.RED, Color.BLUE, Shader.TileMode.CLAMP);
        ComposeShader cs = new ComposeShader(bg, lg, PorterDuff.Mode.SRC_ATOP);
        paint.setShader(cs);
        canvas.drawRect(new Rect(0, 0, getMeasuredWidth(), 600), paint);
    }
}
